package cn.deschen.app.controller;

import cn.deschen.app.vo.KeywordDetailVO;
import cn.deschen.biz.domain.entity.AdKeyword;
import cn.deschen.biz.dto.keyword.KeywordAddDTO;
import cn.deschen.biz.dto.keyword.KeywordQueryDTO;
import cn.deschen.biz.dto.keyword.KeywordUpdateDTO;
import cn.deschen.biz.service.AdKeywordService;
import cn.deschen.common.api.BizResCode;
import cn.deschen.common.api.CmPage;
import cn.deschen.common.api.CmRes;
import cn.deschen.common.api.CmResCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author hanbin_chen
 * @Description 关键字控制层
 * @DateTime 2021/06/20
 * @Version V1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/ad/keyword")
@Api(tags = "关键字管理")
public class AdKeywordController {

    @Autowired
    private AdKeywordService keywordService;

    @Autowired
    private MapperFacade mapperFacade;

    @ApiOperation("创建关键字")
    @PostMapping()
    public CmRes<Void> add(@Valid @RequestBody KeywordAddDTO addDTO) {
        boolean ifExisted = checkIfExisted(addDTO.getKeywordName());
        if (ifExisted) {
            return CmRes.failure(CmResCode.REPEAT_OPERATION.extra("该关键字已存在"));
        }
        AdKeyword keyword = mapperFacade.map(addDTO, AdKeyword.class);
        keywordService.save(keyword);
        return CmRes.success();
    }

    private boolean checkIfExisted(String keywordName) {
        QueryWrapper<AdKeyword> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AdKeyword::getKeywordName, keywordName);
        AdKeyword keyword = keywordService.getOne(wrapper);
        if (null != keyword) {
            return true;
        }
        return false;
    }

    @ApiOperation("更新关键字")
    @PutMapping()
    public CmRes<Void> update(@Valid @RequestBody KeywordUpdateDTO updateDTO) {
        AdKeyword keyword = keywordService.getById(updateDTO.getId());
        if (null == keyword) {
            return CmRes.failure(CmResCode.DATA_NOT_FOUND);
        }
        if (!keyword.getKeywordName().equals(updateDTO.getKeywordName())) {
            boolean ifExisted = checkIfExisted(updateDTO.getKeywordName());
            if (ifExisted) {
                return CmRes.failure(BizResCode.KEYWORD_IS_REGISTERED);
            }
        }

        AdKeyword newKeyword = mapperFacade.map(updateDTO, AdKeyword.class);
        keywordService.updateById(newKeyword);
        return CmRes.success();
    }

    @ApiOperation("删除关键字")
    @DeleteMapping()
    public CmRes<Void> delete(@RequestParam List<Long> ids) {
        keywordService.removeByIds(ids);
        return CmRes.success();
    }

    @ApiOperation("获取关键字列表")
    @GetMapping("/page")
    public CmRes<CmPage<AdKeyword>> page(KeywordQueryDTO queryDTO,
                                         @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                         @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        QueryWrapper<AdKeyword> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(AdKeyword::getUpdateTime);
        if (!StringUtils.isEmpty(queryDTO.getKeywordName())) {
            wrapper.lambda().like(AdKeyword::getKeywordName, queryDTO.getKeywordName());
        }
        List<AdKeyword> keywordList = keywordService.list(wrapper);
        return CmRes.success(CmPage.restPage(keywordList));
    }

    @ApiOperation("根据主键获取关键字详情")
    @GetMapping("/detail")
    public CmRes<KeywordDetailVO> detail(@ApiParam("关键字id") @RequestParam Long id) {

        AdKeyword keyword = keywordService.getById(id);
        KeywordDetailVO detailVO = mapperFacade.map(keyword, KeywordDetailVO.class);
        return CmRes.success(detailVO);
    }
}


